package com.jdyfy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jdyfy.entity.Notification;
import com.jdyfy.result.Result;
import com.jdyfy.service.NotificationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/jdyfy")
@Slf4j
public class NotificationController {

    @Autowired
    private NotificationService notificationService;

    @GetMapping("/notificationsPage")
    public Result<IPage<Notification>> getNotificationsPage(@RequestParam Integer pageNum,
                                                            @RequestParam Integer pageSize,
                                                            @RequestParam String departmentName,
                                                            @RequestParam String message,
                                                            @RequestParam Integer isRead) {
        //创建分页对象
        IPage<Notification> page = new Page<>(pageNum, pageSize);
        //创建查询对象
        QueryWrapper<Notification> queryWrapper = new QueryWrapper<>();

        if (message != null && !message.isEmpty()) {
            queryWrapper.like("message", message);
        }

        if (isRead != -1) {
            queryWrapper.eq("is_read", isRead);
        }

        queryWrapper.eq("department_name", departmentName);

        //按序号倒叙排序
        queryWrapper.orderByDesc("noti_id");

        //执行查询
        IPage<Notification> IPage = notificationService.page(page, queryWrapper);
        return Result.success(IPage);
    }

    //添加或修改通知
    @PostMapping("/notification/save")
    public boolean addOrUpdateNotification(@RequestBody Notification notification) {
        return notificationService.saveOrUpdate(notification);
    }
}

